package Part2_Class理解;

import Part1_反射调用私有结构.Person;
import org.junit.Test;

import java.lang.annotation.ElementType;

/**
 * ClassName: ClassTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.05.02 1:05
 */
public class ClassTest {
    @Test
    public void test1() throws ClassNotFoundException {
        //1、获取运行时类Person.class
        Class<Person> class1 = Person.class;
        System.out.println(class1);

        //2、通过类的实例获取类person.getClass()
        Person person = new Person();
        Class<? extends Person> class2 = person.getClass();

        //3、Class类的静态方法获取Class.forName(className)
        String className = "Part1_反射调用私有结构.Person";//全类名
        Class<?> class3 = Class.forName(className);

        //运行时类在内存中会缓存起来，在整个执行期间，类只会加载一次
        System.out.println(class1 == class2);//true
        System.out.println(class3 == class2);//true


        //4、类的加载器获取(了解)
        Class<?> class4 = ClassLoader.getSystemClassLoader().loadClass("Part1_反射调用私有结构.Person");
        System.out.println(class4 == class2);//true
    }

    @Test
    public void test2() {
        Class<Object> c1 = Object.class;
        Class c2 = Comparable.class;
        Class c3 = String[].class;
        Class c4 = int[][].class;
        Class c5 = ElementType.class;
        Class c6 = Override.class;
        Class c7 = int.class;
        Class c8 = void.class;
        Class c9 = Class.class;

        int[] a = new int[10];
        int[] b = new int[100];
        Class<? extends int[]> c10 = a.getClass();
        Class<? extends int[]> c11 = b.getClass();
        // 只要元素类型与维度一样，就是同一个Class
        System.out.println(c10 == c11);//true
    }
}
